<?php
class Reservation extends CI_Model {
    public $id;
    public $dateRes;
    public $nbPlaces;
    public $id_client;
    public $id_ligne_voyage;
    
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    public function save($id_client, $id_ligne_voyage, $nbPlaces)
    {
        $Date= date("Y-m-j H:i:s");    
        $sql = "INSERT INTO reservation (dateRes, nbPlaces, id_client, id_ligne_voyage) VALUES(".$this->db->escape($Date).",".$this->db->escape($nbPlaces).",".$this->db->escape($id_client).",".$this->db->escape($id_ligne_voyage).")";
            
            try {
                $query = $this->db->query($sql);
                $res=$this->getReservation($id_client,$id_ligne_voyage);
                return $res ? $res->id : null;
            } catch (Exception $e) {
                return false;
            }
    }
    public function getReservation($id_client, $id_ligne_voyage)
    {
        $sql="select * from reservation where id_ligne_voyage=".$this->db->escape($id_ligne_voyage)." and id_client=".$this->db->escape($id_client)." ORDER BY ID DESC;" ;
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0)
        {
            return $query->first_row("Reservation");
        }else{
           return null;
        }
    }
    
    public function getReservationById($id)
    {
        $sql="select * from reservation where id=".$this->db->escape($id)." ORDER BY ID DESC;" ;
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0)
        {
            return $query->first_row("Reservation");
        }else{
           return null;
        }
    }
    
    function getLigneVoyage()
    {
        $query = $this->db->query('select * from ligne_voyage where id='.$this->id_ligne_voyage);
        if ($query->num_rows() > 0)
        {
            return $query->first_row("LigneVoyage");
        }else{
           return null;
        }
    }
    
    function getClient()
    {
        $query = $this->db->query('select * from client where id='.$this->id_client);
        if ($query->num_rows() > 0)
        {
            return $query->first_row("Client");
        }else{
           return null;
        }
    }
}
?>